package main

import (
	"CacheRedis/common"
	"CacheRedis/dislock"
	"CacheRedis/rank"
	"context"
	"fmt"
	"github.com/go-redis/redis/v8"
	"github.com/pkg/errors"
	"log"
)

var ctx = context.Background()

func main() {
	//获取redis实例
	rdb := common.GetRdb()
	defer rdb.Close()
	//基本操作
	err := testData(rdb)
	if err != nil {
		log.Printf("%+v", err)
		return
	}
	//排行榜demo
	err = rank.RankingDemo(rdb)
	if err != nil {
		log.Printf("%+v", err)
		return
	}
	//分布式锁实现demo
	dislock.Task(rdb)
}

func testData(rdb *redis.Client) error {
	//添加值
	err := rdb.Set(ctx,"key1", "value1", 0).Err()
	if err != nil {
		return errors.Wrap(err,"ERR: fail to set key")
	}
	//获取值
	res, err := rdb.Get(ctx,"key1").Result()
	if err != nil {
		return errors.Wrap(err,"ERR: fail to get key")
	}
	//打印值
	fmt.Println(res)
	err = rdb.Del(ctx,"key1").Err()
	if err != nil {
		return errors.Wrap(err,"ERR: fail to delete key")
	}
	return nil
}
